//
//  LadingCarMapVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/14.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LadingCarMapVC.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "LadingCarMapInfoModel.h"
#import "LadingCarInfoView.h"
#import "LadingCarGoodListVC.h"

@interface LadingCarMapVC ()<MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) NSArray <LadingCarMapInfoModel *>*dataArray;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *noticeLab;

@end

@implementation LadingCarMapVC

-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - 268, ScreenW, 269)];
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

-(UILabel *)noticeLab {
    if (!_noticeLab) {
        _noticeLab = [[UILabel alloc] initWithFrame:CGRectMake(15, ScreenH - BottomHeight - 268 - 3 - 20, 90, 20)];
        _noticeLab.textColor = HexColor(0xffffff);
        _noticeLab.font = BoldFontOfSize(12);
        _noticeLab.backgroundColor = Color_ThemeColor;
        _noticeLab.textAlignment = NSTextAlignmentCenter;
        _noticeLab.text = @"提货车辆信息";
        [_noticeLab setBorderWithCornerRadius:5 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight];
        [_noticeLab setBorderWithCornerRadius:10 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerTopRight];
    }
    return _noticeLab;
}

-(NSArray<LadingCarMapInfoModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

-(UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, StatusBarHeight + 7, 44, 44)];
        [_backBtn setImage:[UIImage imageNamed:@"icon_map_back"] forState:0];
        [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)backClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

-(MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _mapView.delegate = self;
    }
    return _mapView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.noticeLab];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryTiHuoCarInfo params:@{@"dp_id":self.dp_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.dataArray = [LadingCarMapInfoModel mj_objectArrayWithKeyValuesArray:responseObject];
            [self configScrollView];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)configScrollView {
    self.scrollView.contentSize = CGSizeMake(15 + ((ScreenW - 30) + 5) * self.dataArray.count + 10, 268);
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __block CGFloat left = 15;
    CGFloat width = ScreenW - 30;
    CGFloat height = 268;
    [self.dataArray enumerateObjectsUsingBlock:^(LadingCarMapInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LadingCarInfoView *view = [[LadingCarInfoView alloc] initWithFrame:CGRectMake(left, 0, width, height)];
        view.model = obj;
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 6;
        __weak_self;
        view.listBlock = ^{
            LadingCarGoodListVC *vc = [LadingCarGoodListVC new];
            vc.Id = obj.Id;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        [self.scrollView addSubview:view];
        left = view.nim_right + 5;
    }];
}

@end
